package com.ycfmedical.bed.ui.fragment;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlConstants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.adapter.TimerControlAdapter;
import com.ycfmedical.bed.local.entity.TimerControl;
import com.ycfmedical.bed.present.TimerControlPresenter;
import com.ycfmedical.bed.widgets.DialogIosSheet;
import com.ycfmedical.bed.widgets.StateView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by Administrator on 2017-03-24.
 */

public class TimingControlFragment extends XFragment<TimerControlPresenter> {
    // 定时提醒列表
    TimerControlAdapter adapter;

    @BindView(R.id.timing_list)
    XRecyclerContentLayout timingList;
    @BindView(R.id.new_timing)
    Button newTiming;
    StateView errorView;
    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        getP().loadData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_timingcontrol;
    }

    @Override
    public TimerControlPresenter newP()
    {
        return new TimerControlPresenter(context);
    }

    public static TimingControlFragment newInstance()
    {
        return new TimingControlFragment();
    }

    private void initAdapter()
    {
        setLayoutManager(timingList.getRecyclerView());

        timingList.getRecyclerView().setAdapter(getAdapter());

        timingList.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener()
                {
                    @Override
                    public void onRefresh()
                    {
                        // getP().handleMessage(Constants.DEFAULT_PAGE, getType());
                    }

                    @Override
                    public void onLoadMore(int page)
                    {
                        // getP().handleMessage(page, getType());
                    }

                });

        if (errorView == null)
        {
            errorView = new StateView(context);
        }
        timingList.errorView(errorView);
        timingList.loadingView(View.inflate(context, R.layout.view_loading, null));

        timingList.getRecyclerView().useDefLoadMoreView();
    }
    public SimpleRecAdapter getAdapter()
    {
        if (adapter == null)
        {
            adapter = new TimerControlAdapter(context);

            // 设置点击事件，点击Item
            adapter.setRecItemClick(new RecyclerItemCallback<TimerControl, TimerControlAdapter.ViewHolder>()
            {
                @Override
                public void onItemClick(int position, TimerControl model, int tag,
                                        TimerControlAdapter.ViewHolder holder)
                {
                    //单击
                }

                @Override
                public void onItemLongClick(int position, TimerControl model, int tag,
                                            TimerControlAdapter.ViewHolder holder)
                {
                    //长安删除
                    super.onItemLongClick(position, model, tag, holder);
                    switch (tag)
                    {
                        case TimerControlAdapter.TAG_TIMES:
                            TextView controlTime = holder.getControlTime();
                            String times = controlTime.getText().toString();
                            getP().deleteShareData(Constants.CONTROL_TIMING);
                            Toast.makeText(context, "长按删除该定时任务", Toast.LENGTH_LONG).show();
                            break;
                    }

                }
            });
        }
        return adapter;
    }

    public void setLayoutManager(XRecyclerView recyclerView)
    {
        recyclerView.verticalLayoutManager(context);
    }

    @OnClick({R.id.timing_list, R.id.new_timing})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.timing_list:
                break;
            case R.id.new_timing:
                new DialogIosSheet(context)
                        .builder()
                        .setTitle("请选择要保存的体位")
                        .addSheetItem(ControlConstants.POST_SLEEP, DialogIosSheet.SheetItemColor.Blue, new DialogIosSheet.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //睡眠体位
                                getP().TimePicker(1);
                            }
                        })
                        .addSheetItem(ControlConstants.POST_RELAXATION, DialogIosSheet.SheetItemColor.Blue, new DialogIosSheet.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //休闲体位
                                getP().TimePicker(2);
                            }
                        })
                        .addSheetItem(ControlConstants.POST_REPAST, DialogIosSheet.SheetItemColor.Blue, new DialogIosSheet.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //就餐体位
                                getP().TimePicker(3);
                            }
                        })
                        .addSheetItem(ControlConstants.POST_WORK, DialogIosSheet.SheetItemColor.Blue, new DialogIosSheet.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //工作体位
                                getP().TimePicker(4);
                            }
                        }).show();
                break;
        }
    }
}

